﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Launch.ViewModel;
using Xceed.Wpf.Toolkit.PropertyGrid;

namespace Launch.Views;

public partial class TestPropertyView : UserControl
{
    // ViewModel
    private readonly TestPropertyVm _vm = new();

    // 属性字典
    Dictionary<string, object> _properties = new();

    public TestPropertyView()
    {
        InitializeComponent();
        DataContext = _vm;

        _properties["Person"] = new Person();
        _properties["Product"] = new Product()
        {
            ProductName = ProductName.产品B,
            Price = 99.99m,
            StockQuantity = 50
        };
        _properties["Settings"] = new Settings();

        _vm.MySelectedObject = _properties["Person"];
        // 属性值改变事件
        MyPropertyGrid.PropertyValueChanged += (s, e) =>
        {
            if (e.OriginalSource is PropertyItem property)
            {
                var changedProperty = property.DisplayName;
                var newValue = e.NewValue;
                Console.WriteLine($"属性 {changedProperty} 已更改为: {newValue}");
            }
        };

        // 选择对象改变事件
        MyPropertyGrid.SelectedObjectChanged += (s, e) =>
        {
            if (e.NewValue != null)
            {
                Console.WriteLine($"选择了新对象: {e.NewValue.GetType().Name}");
            }
        };
    }

    private void Person_Click(object sender, RoutedEventArgs e)
    {
        _vm.MySelectedObject = _properties["Person"];
    }

    private void Product_Click(object sender, RoutedEventArgs e)
    {
        _vm.MySelectedObject = _properties["Product"];
    }

    private void Setting_Click(object sender, RoutedEventArgs e)
    {
        _vm.MySelectedObject = _properties["Settings"];
    }

    private void PersonNameChanged_Click(object sender, RoutedEventArgs e)
    {
        if (_properties["Person"] is Person person)
        {
            person.Name = "王五";
        }
    }
}